//
//  TempMaskForSplicingView.m
//  TSDemo_ImageFilter
//
//  Created by qian on 2021/3/5.
//

#import "TempMaskForSplicingView.h"

@implementation TempMaskForSplicingView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupViews];
        
        _radius = 120;
    }
    return self;
}

- (void)setupViews {
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    //maskLayer.path = path.CGPath;
    maskLayer.fillRule = kCAFillRuleEvenOdd;
    maskLayer.fillColor = [[UIColor blackColor] CGColor];
    maskLayer.opacity = 0.5;
    [self.layer addSublayer:maskLayer];
    _maskLayer = maskLayer;
}




- (void)updateRadius:(UIColor *)clearRectangleRatio
{
//    CGFloat maxRadius = self.frame.size.width/2;
//    self.radius= MIN(self.radius, maxRadius);
//    
//    // 获取空心区域的矩形大小
//    CGFloat clearRectangleWidth = self.radius * 2;
//    CGFloat clearRectangleHeight = self.radius * 2;
//    CGFloat clearRectangleOriginX = CGRectGetWidth(rect)/2 - clearRectangleWidth/2;
//    CGFloat clearRectangleOriginY = CGRectGetHeight(rect)/2 - clearRectangleHeight/2;
//    CGRect clearRectangleFrame = CGRectMake(clearRectangleOriginX, clearRectangleOriginY, clearRectangleWidth, clearRectangleHeight);
//    self.clearRectangleFrame = clearRectangleFrame;
    
    [self setNeedsDisplay];
}

#pragma mark - DrawRect
- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    CGFloat maxRadius = self.frame.size.width/2;
    _radius= MIN(self.radius, maxRadius);
    
    // 获取空心区域的矩形大小
    CGFloat clearRectangleWidth = self.radius * 2;
    CGFloat clearRectangleHeight = self.radius * 2;
    CGFloat clearRectangleOriginX = CGRectGetWidth(rect)/2 - clearRectangleWidth/2;
    CGFloat clearRectangleOriginY = CGRectGetHeight(rect)/2 - clearRectangleHeight/2;
    CGRect clearRectangleFrame = CGRectMake(clearRectangleOriginX, clearRectangleOriginY, clearRectangleWidth, clearRectangleHeight);
    _clearRectangleFrame = clearRectangleFrame;

    UIBezierPath *path= [UIBezierPath bezierPathWithRect:rect];
    UIBezierPath *clearPath = [self __getClearPathFromClearRectangleFrame:clearRectangleFrame clearRectangleType:self.clearRectangleType]; // 获取空心区域/镂空区域的path路径
    [path appendPath:clearPath];
    [path setUsesEvenOddFillRule:YES];
    
    self.maskLayer.path = path.CGPath;
}


#pragma mark - Private Method
/*
 *  获取空心区域/镂空区域的path路径
 *
 *  @param rectangleFrame       空心区域的矩形大小
 *  @param clearRectangleType   空心区域的形状
 *
 *  @return 空心区域/镂空区域的path路径
 */
- (UIBezierPath *)__getClearPathFromClearRectangleFrame:(CGRect)clearRectangleFrame
                                     clearRectangleType:(CJRectPathType)clearRectangleType
{
    UIBezierPath *clearPath = [UIBezierPathCJHelper bezierPathForRectFrame:clearRectangleFrame
                                                                  pathType:clearRectangleType];  // 空心的透明的path
    return clearPath;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
